package com.demo.java.OD351_400.OD375;

import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【分割数组的最大差值(B卷)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146233604
 */
public class OdMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean firstLine = true;
        int n = 0;
        int[] arr = null;

        // 读取输入数据
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (firstLine) {
                firstLine = false;
                n = Integer.parseInt(line); // 第一个输入为数组长度
            } else {
                String[] tokens = line.split(" ");
                arr = new int[tokens.length]; // 第二个输入为数组元素
                for (int i = 0; i < tokens.length; i++) {
                    arr[i] = Integer.parseInt(tokens[i]);
                }
                break;
            }
        }

        if (arr != null) {
            int[] newArr = new int[n];
            System.arraycopy(arr, 0, newArr, 0, n); // 将输入的数组复制到 newArr

            int maxDifference = 0;
            // 通过遍历不同的分割点计算差值
            for (int x = 1; x < newArr.length; x++) {
                int[] leftPart = new int[x];
                int[] rightPart = new int[newArr.length - x];
                System.arraycopy(newArr, 0, leftPart, 0, x); // 左子数组
                System.arraycopy(newArr, x, rightPart, 0, newArr.length - x); // 右子数组

                int leftSum = 0;
                int rightSum = 0;
                // 计算左子数组的和
                for (int num : leftPart) {
                    leftSum += num;
                }
                // 计算右子数组的和
                for (int num : rightPart) {
                    rightSum += num;
                }

                // 计算差值并更新最大差值
                int difference = Math.abs(leftSum - rightSum);
                maxDifference = Math.max(maxDifference, difference);
            }

            // 输出最大差值
            System.out.println(maxDifference);
        }
    }
}